package nancal.mp.db.mongo.base.shop

import nancal.mp.db.mongo.*
import nancal.mp.db.mongo.entity.shop.ShopActivityInfo
import nancal.mp.db.mongo.entity.shop.ShopCouponInfo
import nbcp.base.comm.StringKeyMap
import nbcp.base.comm.StringMap
import nbcp.base.db.*
import nbcp.base.utils.CodeUtil
import nbcp.myoql.db.CityCodeName
import java.time.LocalDateTime


/**
 * 定义 sku 每一项值
 */
data class SkuIValueData(
        var text: String = "",
        var logo: IdUrl = IdUrl(),
        var logoStyle: SkuLogoStyleEnum = SkuLogoStyleEnum.Text
)

/**
 * 定义 sku
 */
data class SkuDefineData(
        var title: String = "",
        var values: MutableList<SkuIValueData> = mutableListOf()
)

/**
 * StringMap 是值比较，表示一个Sku商品
 */
class SkuItemData : StringMap() {
    fun toUnitCode(): String {
        return this.entries.sortedBy { it.key }.map { it.value }.joinToString(",")
    }
}

//产品卡片
data class ProductRef(
        var id: String = "",
        var name: String = "",
        var url: String = "",
        var price: Int = 0
) : java.io.Serializable {}


/**
 * 商家设置活动价
 */
class ShopActivityPrice() : ShopActivityInfo() {
    var price: Int = 0
}

//生产商商品表。
data class ProductSkuStockPrice(
        var skuItem: SkuItemData = SkuItemData(), //逗号分隔的字符串
        var stock: Int = 0,
        var saleCount: Int = 0,        //销量,支付成功后更新。
        var activities: MutableList<ShopActivityPrice> = mutableListOf(),    //当前参加的活动，实时数据

        var guidePrice: Int = 0,        //官方价格：分
        var normalPrice: Int = 0,       //售价：分。

        var coupons: MutableList<ShopCouponInfo> = mutableListOf()
) {

    val salePrice: Int
        get() {
            var list = mutableListOf(guidePrice, normalPrice)

            list.add(activities.map { it.price }.filter { it > 0 }.sum())

            return list.filter { it > 0 }.minOrNull() ?: 0;
        }
}

//内嵌实体
data class ReceiveAddressDataInfo(
        var id: String = CodeUtil.getCode(),
        var alias: String = "", //别名
        var userName: String = "",
        var city: CityCodeName = CityCodeName(),
        var address: String = "",
        var mobile: String = "",
        var email: String = "",
        var zipcode: String = "",
        var longitude: Long = 0L,   //经
        var latitude: Long = 0L, //纬
        var default: Boolean = false,
        var createAt: LocalDateTime = LocalDateTime.now()) {}


data class ProductCardData(
        var categoryPath: MutableList<IdCodeName> = mutableListOf()
) : IdNameUrl()

/**
 * 购买的商品信息
 */
data class OrderInfo_ProductItem(
        var id: String = CodeUtil.getCode(),
        var corp: IdName = IdName(),
        var product: ProductCardData = ProductCardData(),
        var skuItem: SkuItemData = SkuItemData(),         // 如: 金,128G
        var number: Int = 0, //数量
        var price: Int = 0,  //实际单价
        var freight: Int = 0, //运费
        var score: Int = 0     //获得积分/豆,在支付后获得，在退款后置为0.
) {
    val amount: Int
        get() = number * price;

}

/**
 * 活动规则
 */
data class ShopActivityRuleData(
        var memberLevel: MutableList<Int> = mutableListOf(),                //限会员级别
        var clients: MutableList<ClientTypeEnum> = mutableListOf(),        //限使用端
        var categoryPath: MutableList<IdCodeName> = mutableListOf(),     //限品类
        var products: MutableList<IdName> = mutableListOf(),                //限商品
        var cities: MutableList<CodeName> = mutableListOf(),                //限区域
        var payTypes: MutableList<PayTypeEnum> = mutableListOf(),           //限支付渠道
        var favorite: Boolean = false,                                      //限： 非自营且关注商家

        var startAt: LocalDateTime? = null,
        var endAt: LocalDateTime? = null,

        var platformNoCommission: Boolean = false,       //活动期间平台不提成
        var proxyNoCommission: Boolean = false,          //活动期间代理商不提成
        var sallerNoCommission: Boolean = false          //活动期间分销不提成
)

/**
 * 个人发票
 */
open class BaseInvoiceData(
        var id: String = CodeUtil.getCode(),
        var name: String = "",           //个人名称,或,企业名称
        var type: InvoiceTypeEnum = InvoiceTypeEnum.Person,
        var content: InvoiceContentEnum = InvoiceContentEnum.Detail,
        var default: Boolean = false
)

open class CorpInvoiceData(
        var creditNumber: String = ""   //统一社会信用代码/纳税人识别码
) : BaseInvoiceData()

/**
 * 增值税发票
 */
data class VATInvoiceData(
        var address: String = "",       //注册地址
        var phone: String = "",         //注册电话
        var bankName: String = "",      //开户银行
        var bankAccount: String = ""    //银行帐户

        /**
         * 三个照片用于审核
         */
//        var license: IdUrl = IdUrl(),       //营业执照
//        var bankImage: IdUrl = IdUrl(),     //开户行证明.
//        var taxpayer: IdUrl = IdUrl()       //一般纳税人证明.
) : CorpInvoiceData()

/**
 * 支付信息
 */
data class OrderPayInfo(
        var remark: String = "", //备注
        var payType: PayTypeEnum = PayTypeEnum.Alipay, //支付方式
        // var invoice: InvoiceData = InvoiceData(), //发票信息
        var payAmount: Int = 0,     //支付总金额(含运费,扣除优惠.)
        var freight: Int = 0,       //运费
        var payAt: LocalDateTime? = null,  //付款时间，回调时更新
        var paySysMessage: String = ""     //支付回调消息.
)


data class ExpressTraceData(
        var cityName: String = "", //当前城市
        var deliveryman: String = "", //送货人名称
        var mobile: String = "",  //当前人手机号
        var status: DeliveryStatusEnum? = null, //快递状态
        var createAt: LocalDateTime = LocalDateTime.now()
)

data class OrderDeliveryData(
        var expressCorp: IdName = IdName(), //快递公司
        var expressNumber: String = "",     //快递单号
        var sentAt: LocalDateTime? = null,   //发货时间
        var receiveAt: LocalDateTime? = null, //收货时间
        var deliveryBox: String = "",   //自提柜
        var deliveryCode: String = "",  //提货码
        var deliveryman: String = "",
        var deliverymanMobile: String = "",
        var traces: MutableList<ExpressTraceData> = mutableListOf()  //快递跟踪信息
)


/**
 * 退款数据
 */
data class RefundData(
        var product: IdNameUrl = IdNameUrl(),
        var number: Int = 0,
        var payAmount: Int = 0,       //支付金额 = 应退金额
        var refundAmount: Int = 0,    //退款金额
        var reason: String = "",      //退款原因
        var mobile: String = "",       //联系人手机
        var userName: String = "",      //联系人
        var images: MutableList<IdUrl> = mutableListOf(),   //退货凭证
        var status: RefundStatusEnum = RefundStatusEnum.Created,  // 退款状态 , true 退款成功
        var corpApply: String = "",    //商家回应
        var deliveryInfo: OrderDeliveryData = OrderDeliveryData()
)


/**
 * 分销提成数据
 */
open class CommissionData(
        var partner: IdNameUrl = IdNameUrl(),    //分销人
        var billCommissionAmount: Int = 0,      //应提成金额
        var harvestAmount: Int = 0,             //已提成金额
        var createAt: LocalDateTime = LocalDateTime.now()
)

/**
 * 物流评价
 */
data class ExpressEvaluateData(
        var packScore: Int = 0,
        var transportSpeedScore: Int = 0,
        var deliveryServiceScore: Int = 0
)

data class ProductParameterData(
        var title: String = "",
        var values: MutableList<ProductParameterDataItem> = mutableListOf()
)

/**
 * 商品参数的每一项
 */
data class ProductParameterDataItem(
        var key: String = "",
        var value: String = "",
        var remark: String = ""
)

/**
 * 样式定义， key = selector , value 是样式表
 */
class StyleDefineData() : StringKeyMap<StringMap>()

/**
 * 评论数据
 */
data class DiscussItem(
        var content: String = "",
        var images: MutableList<IdUrl> = mutableListOf(),   //晒单
        var upCount: Int = 0,        //获赞数
        var downCount: Int = 0,      //获踩数
        var createBy: IdNameUrl = IdNameUrl(),
        var anonymous: Boolean = false,         //匿名评论

        var discussions: MutableList<DiscussItem> = mutableListOf(),     //跟贴讨论区
        var createAt: LocalDateTime = LocalDateTime.now()
)

/**
 * 评价
 */
data class EvaluateData(
        var expressEvaluate: ExpressEvaluateData = ExpressEvaluateData(),
        var productScore: Int = 0,
        var discussion: DiscussItem = DiscussItem()             //主要论述,贴子内容
)

